package cn.edu.cimv.controller;

import cn.edu.cimv.constant.InfoConstant;
import cn.edu.cimv.dao.entity.Member;
import cn.edu.cimv.dao.entity.MemberItem;
import cn.edu.cimv.dao.entity.User;
import cn.edu.cimv.pojo.Message;
import cn.edu.cimv.pojo.PaperCallBack;
import cn.edu.cimv.service.UploadService;
import cn.edu.cimv.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by 门铎 on 2016/7/26.
 */
@Controller
@RequestMapping("/cimv/user/")
public class UserController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private UploadService uploadService;
    @Autowired
    private Message message;
    @RequestMapping("list")
    public String userList(Model model){
        List<User> userList = userService.queryUsers();
        model.addAttribute("userList",userList);
        return "user/user-control";
    }

    @RequestMapping("new")
    public String userNew(){
        return "user/user-new";
    }

    @RequestMapping("create")
    public  String userCreate(@RequestParam("username")String userName,
                              @RequestParam("password")String password,
                              @RequestParam("role")String role,
                              Model model){
        int flag = userService.insertUser(userName,password,role);
        if(flag == 0){
            model.addAttribute("msg", InfoConstant.CREATE_USER_SUCCESS);
            return "user/success";
        }else{
            model.addAttribute("username",userName);
            model.addAttribute("password",password);
            model.addAttribute("role",role);
            if(flag ==1){
                model.addAttribute("msg",InfoConstant.ADMIN_HAS_ONE);
            }else{
                model.addAttribute("msg",InfoConstant.USER_HAS_ONE);
            }

            return "user/fail";
        }
    }

    @RequestMapping(value = "delete/{id}")
    public String userDelete(@PathVariable("id")int id,
                             Model model){
        userService.deleteUser(id);
        model.addAttribute("msg","<b>"+id+ "号用户</b>" + InfoConstant.USER_DELERE_SUCCESS);
        return "forward:/cimv/user/list";
    }
    @RequestMapping(value = "update/{id}")
    public String updateUser(@PathVariable("id")int id,
                             Model model){
        User user = userService.queryUserById(id);
        model.addAttribute("user",user);

        return "user/user-update";
    }
    @RequestMapping(value = "updateItem/{id}")
    public String updateItemUser(@PathVariable("id")int id,
                                 @RequestParam("username")String userName,
                                 @RequestParam("password")String password,
                                 @RequestParam("role")String role,
                                 Model model){
        int flag = userService.updateUser(userName,password,role,id);
        if(flag == 0){
            model.addAttribute("msg", InfoConstant.USER_UPDATE_SUCCESS);
            return "user/success";
        }else{
            model.addAttribute("username",userName);
            model.addAttribute("password",password);
            model.addAttribute("role",role);
            model.addAttribute("msg",InfoConstant.USER_UPDATE_FAIL);
            return "user/user-update";
        }
    }


    @RequestMapping(value = "details/{userId}")
    public String details(Model model,@PathVariable("userId")int userId){
        model.addAttribute("userId",userId);
        Member member = userService.selectMember(userId);
        model.addAttribute("member",member);
        List<MemberItem> memberItemList = userService.selectMemberItems(String.valueOf(userId));
        model.addAttribute("memberItems",memberItemList);
        //根据id查询简历列表
        return "user/details-update";
    }

    @RequestMapping(value = "updateDetails")
    public @ResponseBody Message uploadDetails(Member member){
        userService.updateMember(member);
        message.setMsg(InfoConstant.UPDATE_NORMAL_SUCCESS);
        return message;
    }
    /**
     * ajax上传头像 返回头像地址
     * @param tou
     * @param request
     * @return
     */
    @RequestMapping(value = "tou",method = RequestMethod.POST)
    public @ResponseBody String tou(MultipartFile tou, HttpServletRequest request){
        //上传图片
        String rootPath = request.getSession().getServletContext().getRealPath("/");
        String touName  = uploadService.uploadTou(tou,rootPath);
        return touName;

    }

    @RequestMapping(value = "memberItemNew/{userId}",method = RequestMethod.GET)
    public String memberItemNew(@PathVariable("userId")String userId,Model model){
        model.addAttribute("userId",userId);
        return "/user/member-item-new";
    }

    @RequestMapping(value = "memberItemCreate/{userId}",method = RequestMethod.POST)
    public @ResponseBody Message memberItemCreate(@PathVariable("userId")String userId,MemberItem memberItem,Model model){
        userService.insertMemberItem(memberItem);
        message.setMsg(InfoConstant.CREATE_NORMAL_SUCCESS);
        return message;
    }
    @RequestMapping(value = "memberItemUpdateView/{id}",method = RequestMethod.GET)
    public String memberItemUpdate(@PathVariable("id")int id,Model model){
        //根据ID 查到对应列的信息，放入前台
        MemberItem memberItem = userService.getMemberItemsByKey(id);
        model.addAttribute("memberItem",memberItem);
        return "/user/member-item-update";
    }

    @RequestMapping(value = "memberItemUpdate/{id}",method = RequestMethod.POST)
    public @ResponseBody Message memberItemCreate(@PathVariable("id")int id,MemberItem memberItem,Model model){
        userService.updateMemberItem(memberItem);
        message.setMsg(InfoConstant.UPDATE_NORMAL_SUCCESS);
        return message;

    }

    @RequestMapping(value = "memberItemDeleteView/{id}/{userId}",method = RequestMethod.GET)
    public String memberItemCreate(@PathVariable("id")int id,@PathVariable("userId")String userId,Model model){
        userService.deleteMemberItem(id);
        message.setMsg(InfoConstant.DELETE_NORMAL_SUCCESS);
        return "forward:/cimv/user/details/"+userId;

    }

}
